//
//  TransactSeeViewController.m
//  KD
//
//  Created by Hubery on 15/8/17.
//  Copyright (c) 2015年 centling. All rights reserved.
//

#import "TransactSeeViewController.h"
#import "TransactTableViewCell.h"
#import "OddNumberModel.h"
#import "HXDropdownMenu.h"
#import "Menu.h"
#import "OddNumberDetailsViewController.h"
#import "DetailsTableViewController.h"
#import "HXSearchBar.h"
#import "QRCodeViewController.h"
#import "ArrearageViewController.h"

@interface TransactSeeViewController ()<UITableViewDataSource,UITableViewDelegate,HXSearchBarDelegate,QRCodeViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *divisionLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *oddNumbers;//单号假数据
@property(nonatomic,strong) UITextField *searchTextField;
@end

@implementation TransactSeeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.divisionLabel.layer.borderWidth = 1;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UINib *nib = [UINib nibWithNibName:@"TransactTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"TransactCell"];
    self.navigationItem.leftBarButtonItem = [GlobalUtil createBarButtonItemWithImage:@"666" target:self action:@selector(mySetUp) tag:1];
    self.navigationItem.rightBarButtonItem = [GlobalUtil createBarButtonItemWithImage:@"666" target:self action:@selector(menu:) tag:1];
    [self addSearchBar];
    
    
    //__weak UITableView *tableView = self.tableView;
    
    // 设置回调
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    header.autoChangeAlpha = YES;
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    // 马上进入刷新状态
    [header beginRefreshing];
    // 设置header
    self.tableView.header = header;
}

- (void)loadNewData
{
    // 1.添加假数据
    for (int i = 0; i<5; i++) {
        //
    }
    
    // 2.模拟2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        //[self.tableView reloadData];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.header endRefreshing];
    });
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.oddNumbers.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   TransactTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TransactCell" forIndexPath:indexPath];
    cell.oddNumberModel = self.oddNumbers[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OddNumberModel *model = self.oddNumbers[indexPath.row];
    NSString *state = model.oddNumberState;
    if ([state  isEqual: @"未付款"]) {
       ArrearageViewController *vc = [[ArrearageViewController alloc]init];
       vc.oddNumber  = self.oddNumbers[indexPath.row];
       [self.navigationController pushViewController:vc animated:YES];
    }else if ([state  isEqual: @"逾期"]){
       DetailsTableViewController *vc = [[DetailsTableViewController alloc]init];
       vc.oddNumber  = self.oddNumbers[indexPath.row];
       [self.navigationController pushViewController:vc animated:YES];
    }else if ([state  isEqual: @"进行中"]){
        
    }else if ([state  isEqual: @"未通关"]){
        
    }else{//完成
        
    }

}

#pragma mark - HXSearchBarDelegate
- (void)searchBarDidQrCode:(HXSearchBar *)searchBar
{
    QRCodeViewController *vc = [[QRCodeViewController alloc]init];
    vc.delegate = self;
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark - QRCodeViewControllerDelegate
-(void)numberWithString:(NSString *)str
{
    self.searchTextField.text = str;
}

#pragma mark - 添加SearchBar
-(void)addSearchBar
{
    HXSearchBar *searchBar = [HXSearchBar searchBar];
    searchBar.frame = CGRectMake(5, 66, 310, 40);
    searchBar.searchDelegate = self;
    self.searchTextField = searchBar;
    [self.view addSubview:searchBar];
}

#pragma mark - 添加右上角菜单
-(void)menu:(UIBarButtonItem *)btn
{
    Menu *view = [[[NSBundle mainBundle] loadNibNamed:@"menu" owner:nil options:nil]lastObject];
    view.frame = CGRectMake(100, 0, 160, 200);
    [self.view addSubview:view];
    HXDropdownMenu *menu = [[HXDropdownMenu alloc]init];
    menu.content = view;
    [menu showFrom:(UIView *)btn];
}

#pragma mark - 跳转到设置
-(void)mySetUp
{
    [self performSegueWithIdentifier:@"TransactSetup" sender:nil];
}

#pragma mark - oddNumbers懒加载
- (NSMutableArray *)oddNumbers
{
    if (_oddNumbers == nil) {
        _oddNumbers = [NSMutableArray array];
        NSString *fileName = [[NSBundle mainBundle]pathForResource:@"oddNumber.json" ofType:nil];
        NSData *data = [NSData dataWithContentsOfFile:fileName];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        for (NSDictionary *dict in array) {
            OddNumberModel *model = [OddNumberModel oddNumberWithDict:dict];
            [_oddNumbers addObject:model];
        }
    }
    return  _oddNumbers;
}
@end


